<template>
  <div>
    <!-- SKU列表 -->
    <el-table :data="skuList" style="width: 100%; margin: 20px 0;" border v-loading="loading">
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="skuName" label="名称" width="width" header-align="center"></el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width" header-align="center"></el-table-column>
      <el-table-column prop="prop" label="默认图片" width="120">
        <template slot-scope="scope">
          <img :src="scope.row.skuDefaultImg" style="width:100px; height:100px;">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80"></el-table-column>
      <el-table-column prop="price" label="价格" width="80"></el-table-column>
      <el-table-column prop="prop" label="操作" width="width" align="center">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-top" size="mini" v-if="scope.row.isSale == 1" @click="cancel(scope.row)"></el-button>
          <el-button type="info" icon="el-icon-bottom" size="mini" v-else @click="sale(scope.row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" @click="openDrawer(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteSku(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="pageSizes"
      @current-change="getsKuList"
      style="text-align: center;"
      :current-page="page"
      :page-sizes="[5, 10, 20]"
      :page-size="limit"
      layout="sizes, prev, pager, next, jumper, total"
      :total="total">
    </el-pagination>
    <!-- 抽屉 -->
    <el-drawer
      title="SKU详情"
      :visible.sync="drawer"
      :show-close="false"
      size="40%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{skuInfo.skuName}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{skuInfo.skuDesc}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{skuInfo.price}}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag 
              style="margin-right:10px"
              type="success" 
              v-for="(attr, index) in skuInfo.skuAttrValueList" 
              :key="attr.id">
              {{attr.attrId}}-{{attr.valueId}}
            </el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
           <el-carousel height="150px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="">
              </el-carousel-item>
            </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
    name: 'Sku',
    data(){
      return{
        page: 1,
        limit: 10, //代表当前页面有几条数据
        skuList: [], //存储SKU列表的数据
        total: 0,
        loading: true,
        drawer: false,
        skuInfo: {},
      }
    },
    mounted(){
      //获取sku列表的方法
      this.getsKuList()
    },
    methods: {
      // 获取sku列表
      getsKuList(pages = 1){
        this.page = pages
        const {page, limit} = this
        return new Promise((resolve, reject) => {
          this.$API.sku.reqgetSkuList(page, limit).then((result) => {
            console.log(result)
            if(result.code == 200){
              this.skuList = result.data.records
              this.total = result.data.total
              this.loading = false
            }
            resolve()
          }).catch((err) => {
            this.loading = false
            reject(err)
          });
        })
      },
      // 改变limit，每页显示条数
      pageSizes(limit){
        this.limit = limit
        this.getsKuList()
      },
      // 下架
      cancel(row){
        return new Promise((resolve, reject) => {
          this.$API.sku.reqcancelSale(row.id).then((result) => {
            this.$message({type:"success", message:"下架成功"})
            row.isSale = 0
            resolve()
          }).catch((err) => {
            reject(err)
          })
        })
      },
      // 上架
      sale(row){
        return new Promise((resolve, reject) => {
          this.$API.sku.reqonSale(row.id).then((result) => {
            this.$message({type:"success", message:"上架成功"})
            row.isSale = 1
            resolve()
          }).catch((err) => {
            reject(err)
          })
        })
      },
      // 修改,正在开发中
      edit(row){
        this.$message({type:"info", message:"正在开发中..."})
      },
      // 打开抽屉
      openDrawer(row){
        this.drawer = true
        // 发请求获取sku详情信息
        return new Promise((resolve, reject) => {
          this.$API.sku.reqgetSkuById(row.id).then(res => {
            if(res.code == 200){
              console.log(res)
              this.skuInfo = res.data
            }
            resolve()
          }).catch(err => {
            reject(err)
          })
        })
      },
      // 删除sku
      deleteSku(row){
        this.$confirm('此操作将永久删除该SKU, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          // 发请求删除sku
          let res = await this.$API.sku.reqdeleteSku(row.id)
          if(res.code == 200){
              console.log(res)
              this.$message({type:"success", message:"删除成功"})
              this.getsKuList()
            }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
    },
}
</script>

<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
  .el-carousel__button{
  width:10px;
  height:10px;
  background:red;
  border-radius:50%;
}

</style>

<style scoped>
.el-row .el-col-5{
  font-size:18px;
  text-align:right;
}
.el-col{
  margin:10px 10px;
}
/* <<< css原生选择器, 有scoped，父组件样式想要影响子组件样式需要使用深度选择器*/
/* >>>.el-carousel__button{
  width:10px;
  height:10px;
  background:red;
  border-radius:50%;
} */
</style>